﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Infrastructure.ObjectModel;
using Microsoft.Xna.Framework;

namespace B10_Ex05_Elad_Meyron.Models
{
    public class GameStatusDisplay : LoadableDrawableComponent
    {
        #region Consts
        private const string k_assetName = @"Fonts\PlayerFont";
        private const string k_TextStart = @" Score:";
        private const int k_halfOpacitit = 123;
        private const int k_fontSize = 25;
        private const int k_fontOffset = 5;
        #endregion

        #region Data members
        protected SpriteBatch m_SpriteBatch;
        private bool m_UseSharedBatch = false;
        private SpriteFont m_spfScoreFont;
        private string m_sPlayerScore;

        #endregion

        #region Ctor and Other methods
        /// <summary>
        /// Creates a game status display
        /// </summary>
        /// <param name="i_Game"></param>
        /// <param name="i_humPlayerOne"></param>
        /// <param name="i_humPlayerTwo"></param>
        /// <param name="i_PlayerOneColor"></param>
        /// <param name="i_PlayerTwoColor"></param>
        public GameStatusDisplay(Game i_Game)
            : base(k_assetName, i_Game, 0)
        {
            // Draw after background
            this.DrawOrder = 50;
        }

        public void SetScore(int nScore)
        {
            m_sPlayerScore = k_TextStart + nScore.ToString();
        }

        protected override void InitBounds()
        {
            // not implemented
        }

        protected override void DrawBoundingBox()
        {
            // not implemented
        }

        /// <summary>
        /// Loads the sprite font
        /// </summary>
        protected override void LoadContent()
        {
            // Load the sprite font
            m_spfScoreFont = Game.Content.Load<SpriteFont>(k_assetName);

            // Check the sprite batch
            if (m_SpriteBatch == null)
            {
                // maybe needed to refresh
                m_SpriteBatch =
                    Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;

                // maybe needed to refresh
                if (m_SpriteBatch == null)
                {
                    m_SpriteBatch = new SpriteBatch(Game.GraphicsDevice);
                }
            }

            base.LoadContent();
        }

        /// <summary>
        /// Resets the data for display
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        /// <summary>
        /// Normal draw
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {
            if (!m_UseSharedBatch)
            {
                m_SpriteBatch.Begin();
            }

            // Draw the text
            m_SpriteBatch.DrawString(m_spfScoreFont, m_sPlayerScore, new Vector2(k_fontOffset, 20), Color.Black);
            Game.Window.Title = m_sPlayerScore;

            if (!m_UseSharedBatch)
            {
                m_SpriteBatch.End();
            }

            base.Draw(gameTime);
        } 
        #endregion
    }
}